import Toast from "../../component/vant/toast/toast";

const app = getApp();
const api = require('../../api.js');
const util = require("../../utils/util.js");
var WxParse = require('../../component/wxParse/wxParse.js');
Page({
  /**
   * 页面的初始数据
   */

  data: {
    // domain: api.xh.domain,
    domain: api.xh.downloadDomain,
    avatar: '/image/course-default-cover.png',
    famous_teacher: [],
    stages: [
      { stage_id: 1, stage_name: '小学' },
      { stage_id: 2, stage_name: '初中' },
      { stage_id: 3, stage_name: '高中' }
    ],
    courses: [],
    stageSelect: '',
    courseSelect: '',
    selectStage: '',
    selectSubject: '',
    show: false,
    selected: 0,
    teacherTagStyle: util.teacherTagStyles
  },

  toggleCourse: function() {
    var page = this;
    page.setData({
      show: !page.data.show
    });
  },

  sureChoose: function() {
    var page = this;
    page.setData({
      last_id: 0,
      famous_teacher: [],
      show: false
    });
    page.getTeahcerList();
  },

  chooseStage: function(e) {
    var page = this;
    if (page.data.selected == 0) {
      page.setData({
        selected: 1
      });
    }

    page.setData({
      selectStage:
        e.currentTarget.dataset.select == ''
          ? '全部'
          : e.currentTarget.dataset.name,
      stageSelect: e.currentTarget.dataset.select
    });
  },

  chooseSubject: function(e) {
    var page = this;
    if (page.data.selected == 0) {
      page.setData({
        selected: 1
      });
    }
    page.setData({
      selectSubject:
        e.currentTarget.dataset.select == ''
          ? ''
          : e.currentTarget.dataset.name,
      courseSelect: e.currentTarget.dataset.select
    });
  },

  toTeacherDetail: function(e) {
    var teacher_id = e.currentTarget.dataset.id;
    var teacher_title = e.currentTarget.dataset.title;
    var name = e.currentTarget.dataset.name;
    var logging = wx.getStorageSync('_logining');
    if (!logging) {
      app.toAuth('/' + this.route);
    } else {
      wx.navigateTo({
        url:
          '../teacher_detail/teacher_detail?teacher_id=' +
          teacher_id +
          '&teacher_title=' +
          teacher_title +
          '&name=' +
          name
      });
    }
  },
  //html转wxml处理
  processHtml: function() {
    var page = this;
    var famous_teacher = page.data.famous_teacher;
    var profileArr = [];

    famous_teacher.map((teacher, index) => {
      // famous_teacher[index].seq = index;
      if (teacher.profile != undefined && teacher.profile != '') {
        profileArr[index] = teacher.profile;
      } else {
        profileArr[index] = '暂无简介';
      }
      // WxParse.wxParse('profile' + index, 'html', profileArr[index], page);
      // if (index == famous_teacher.length - 1) {
      //   WxParse.wxParseTemArray("profileTemArray", 'profile', profileArr.length, page);
      // }
    });
    page.setData({
      famous_teacher: famous_teacher
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var page = this;

    //获取课程
    app.sendRequest({
      url: app.api.public.school_course_list,
      success: function(res) {
        var courses = res.data.list;
        courses == undefined ? [] : courses;
        page.setData({
          courses: courses
        });
      }
    });

    page.getTeahcerList();
  },

  frmClick(e) {
    app.saveFormId(e.detail.formId);
  },
  
  getTeahcerList() {
    let page = this;
    let param = {
      stage: page.data.stageSelect == '' ? 0 : page.data.stageSelect,
      course_id: page.data.courseSelect == '' ? 0 : page.data.courseSelect
    };

    if (page.data.last_id > -1 && page.data.last_id != '') {
      param.last_id = page.data.last_id;
    } else {
      if (page.data.last_id == -1) {
        page.data.famous_teacher.length > 0 && Toast('没有更多了');
        return false;
      }
    }

    app.sendRequest({
      url: api.teacher.list,
      data: param,
      success: function(res) {
        var teacher_list = page.data.famous_teacher;
        var teacher_list_load = res.data.list;

        teacher_list_load == undefined ? [] : teacher_list_load;
        var last_id = 0;

        if (teacher_list_load != undefined && teacher_list_load.length > 0) {
          last_id = res.data.last_id;
          teacher_list = teacher_list.concat(teacher_list_load);
          page.setData({
            famous_teacher: teacher_list,
            last_id: last_id
          });
          page.processHtml();
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.getTeahcerList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});